package com.leonzhangxf.controller;

import com.leonzhangxf.domain.Article;
import com.leonzhangxf.service.ArticleService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author leonzhangxf
 */
@Controller
public class ArticleController {

    private final ArticleService articleService;

    public ArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }

    @RequestMapping("/")
    public String articles(
        Model model,
        @RequestParam(value = "page", defaultValue = "0", required = false) Integer pageOffset,
        @RequestParam(value = "size", defaultValue = "2", required = false) Integer size
    ) {
        PageRequest pageRequest = PageRequest.of(pageOffset, size);
        Page<Article> page = articleService.findPageByPageRequest(pageRequest);
        model.addAttribute("page", page);
        return "client/index";
    }
}
